home *** CD-ROM | disk | FTP | other *** search
/ Personal Computer World 2008 February / PCWFEB08.iso / Software / Freeware / Miro 1.0 / Miro_Installer.exe / xulrunner / python / test / subscriptiontest.py < prev    next >
Encoding:
Python Source  |  2007-11-12  |  11.1 KB  |  294 lines

  1. import os
  2. import unittest
  3.  
  4. import subscription
  5.  
  6. from test.framework import DemocracyTestCase
  7.  
  8. # =============================================================================
  9. # Test data
  10. # =============================================================================
  11.  
  12. SAMPLE_RSS_SUBSCRIPTION_URL_1  = "http://www.domain-1.com/videos/rss.xml"
  13. SAMPLE_RSS_SUBSCRIPTION_URL_2  = "http://www.domain-2.com/videos/rss.xml"
  14. SAMPLE_ATOM_SUBSCRIPTION_URL_1 = "http://www.domain-1.com/videos/atom.xml"
  15. SAMPLE_ATOM_SUBSCRIPTION_URL_2 = "http://www.domain-2.com/videos/atom.xml"
  16.  
  17. # -----------------------------------------------------------------------------
  18.  
  19. INVALID_CONTENT_1 = u"""
  20. This is not XML...
  21. """
  22.  
  23. # -----------------------------------------------------------------------------
  24.  
  25. INVALID_CONTENT_2 = u"""\
  26. <?xml version="1.0" encoding="UTF-8" ?>
  27. <this-is-bogus-xml-syntax>
  28.     yes indeed
  29. </this-is-bogus-xml-syntax>
  30. """
  31.  
  32. # -----------------------------------------------------------------------------
  33.  
  34. ATOM_LINK_CONSTRUCT_IN_RSS = u"""\
  35. <?xml version="1.0" encoding="UTF-8" ?>
  36. <rss version="2.0">
  37.     <channel>
  38.         <title>Dummy RSS Feed</title>
  39.         <link>http://www.getdemocracy.com</link>
  40.         <description>A dummy RSS feed to test USM subscription</description>
  41.         <atom:link 
  42.             xmlns:atom="http://www.w3.org/2005/Atom"
  43.             rel="self" 
  44.             type="application/rss+xml" 
  45.             title="Sample Dummy RSS Feed" 
  46.             href="%s" />
  47.     </channel>
  48. </rss>
  49. """ % SAMPLE_RSS_SUBSCRIPTION_URL_1
  50.  
  51. # -----------------------------------------------------------------------------
  52.  
  53. ATOM_LINK_CONSTRUCT_IN_ATOM = u"""\
  54. <?xml version="1.0" encoding="UTF-8" ?>
  55. <feed xmlns="http://www.w3.org/2005/Atom">
  56.     <title>Dummy Atom Feed</title>
  57.     <updated>2006-05-28T11:00:00Z</updated>
  58.     <author><name>Luc Heinrich</name></author>
  59.     <id>urn:uuid:D7732206-B0BF-4FAF-B2CE-FC25C6C5548F</id>
  60.     <link 
  61.         xmlns:atom="http://www.w3.org/2005/Atom"
  62.         rel="self" 
  63.         type="application/rss+xml" 
  64.         title="Sample Dummy Atom Feed" 
  65.         href="%s" />
  66. </feed>
  67. """ % SAMPLE_ATOM_SUBSCRIPTION_URL_1
  68.  
  69. # -----------------------------------------------------------------------------
  70.  
  71. REFLEXIVE_AUTO_DISCOVERY_IN_RSS = u"""\
  72. <?xml version="1.0" encoding="UTF-8" ?>
  73. <rss version="2.0">
  74.     <channel>
  75.         <title>Dummy RSS Feed</title>
  76.         <description>
  77.             A dummy RSS feed to test USM Reflexive Auto Discovery
  78.         </description>
  79.         <link>reflexive-auto-discovery-page-rss.html</link>
  80.     </channel>
  81. </rss>
  82. """
  83.  
  84. REFLEXIVE_AUTO_DISCOVERY_PAGE_RSS_FILENAME = "reflexive-auto-discovery-page-rss.html"
  85. REFLEXIVE_AUTO_DISCOVERY_PAGE_RSS = u"""\
  86. <?xml version="1.0" encoding="utf-8"?>
  87. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" 
  88.                       "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
  89. <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
  90.     <head>
  91.         <meta http-equiv="Content-type" content="text/html; charset=utf-8" />
  92.         <title>Reflexive Auto Discovery Page</title>
  93.         <link rel="alternate" type="application/rss+xml" title="RSS" href="%s" />
  94.     </head>
  95.     <body>
  96.         This place intentionally (almost) blank... :)
  97.     </body>
  98. </html>
  99. """ % SAMPLE_RSS_SUBSCRIPTION_URL_1
  100.  
  101. # -----------------------------------------------------------------------------
  102.  
  103. REFLEXIVE_AUTO_DISCOVERY_IN_ATOM = u"""\
  104. <?xml version="1.0" encoding="UTF-8" ?>
  105. <feed xmlns="http://www.w3.org/2005/Atom">
  106.     <title>Dummy Atom Feed</title>
  107.     <updated>2006-05-28T11:00:00Z</updated>
  108.     <author><name>Luc Heinrich</name></author>
  109.     <id>urn:uuid:D7732206-B0BF-4FAF-B2CE-FC25C6C5548F</id>
  110.     <link 
  111.         xmlns:atom="http://www.w3.org/2005/Atom"
  112.         rel="alternate" 
  113.         type="application/atom+xml" 
  114.         title="Sample Dummy AutoDiscovery Feed" 
  115.         href="reflexive-auto-discovery-page-atom.html" />
  116. </feed>
  117. """
  118. REFLEXIVE_AUTO_DISCOVERY_PAGE_ATOM_FILENAME = "reflexive-auto-discovery-page-atom.html"
  119. REFLEXIVE_AUTO_DISCOVERY_PAGE_ATOM = u"""\
  120. <?xml version="1.0" encoding="utf-8"?>
  121. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" 
  122.                       "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
  123. <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
  124.     <head>
  125.         <meta http-equiv="Content-type" content="text/html; charset=utf-8" />
  126.         <title>Reflexive Auto Discovery Page</title>
  127.         <link rel="alternate" type="application/atom+xml" title="RSS" href="%s" />
  128.     </head>
  129.     <body>
  130.         This place intentionally (almost) blank... :)
  131.     </body>
  132. </html>
  133. """ % SAMPLE_ATOM_SUBSCRIPTION_URL_1
  134.  
  135. # -----------------------------------------------------------------------------
  136.  
  137. OPML_FLAT = u"""\
  138. <?xml version="1.0" encoding="utf-8"?>
  139. <opml version="1.0">
  140.     <head>
  141.         <title>Sample OPML Flat Subscription List</title>
  142.     </head>
  143.     <body>
  144.         <outline text="Sample Dummy RSS Feed 1" type="rss" xmlUrl="%s" />
  145.         <outline text="Sample Dummy Atom Feed 1" type="rss" xmlUrl="%s" />
  146.         <outline text="Sample Dummy RSS Feed 2" type="rss" xmlUrl="%s" />
  147.         <outline text="Sample Dummy Atom Feed 2" type="rss" xmlUrl="%s" />
  148.     </body>
  149. </opml>
  150. """ % (SAMPLE_RSS_SUBSCRIPTION_URL_1, SAMPLE_ATOM_SUBSCRIPTION_URL_1, SAMPLE_RSS_SUBSCRIPTION_URL_2, SAMPLE_ATOM_SUBSCRIPTION_URL_2)
  151.  
  152. OPML_NESTED = u"""\
  153. <?xml version="1.0" encoding="utf-8"?>
  154. <opml version="1.0">
  155.     <head>
  156.         <title>Sample OPML Flat Subscription List</title>
  157.     </head>
  158.     <body>
  159.         <outline text="folder 1">
  160.             <outline text="Sample Dummy RSS Feed 1" type="rss" xmlUrl="%s" />
  161.             <outline text="folder1-1">
  162.                 <outline text="Sample Dummy Atom Feed 1" type="rss" xmlUrl="%s" />
  163.             </outline>
  164.         </outline>
  165.         <outline text="folder 2">
  166.             <outline text="folder2-1">
  167.                 <outline text="Sample Dummy RSS Feed 2" type="rss" xmlUrl="%s" />
  168.             </outline>
  169.             <outline text="Sample Dummy Atom Feed 2" type="rss" xmlUrl="%s" />
  170.         </outline>
  171.     </body>
  172. </opml>
  173. """ % (SAMPLE_RSS_SUBSCRIPTION_URL_1, SAMPLE_ATOM_SUBSCRIPTION_URL_1, SAMPLE_RSS_SUBSCRIPTION_URL_2, SAMPLE_ATOM_SUBSCRIPTION_URL_2)
  174.  
  175. # =============================================================================
  176. # Test case
  177. # =============================================================================
  178.  
  179. class TestSubscription (DemocracyTestCase):
  180.         
  181.     subscription.reflexiveAutoDiscoveryOpener = open
  182.  
  183.     def testInvalidSubscriptions(self):
  184.         urls = subscription.parseFile("this-file-does-not-exist.xml")
  185.         self.assert_(urls is None)
  186.         urls = subscription.parseContent(INVALID_CONTENT_1)
  187.         self.assert_(urls is None)
  188.         urls = subscription.parseContent(INVALID_CONTENT_2)
  189.         self.assert_(urls is None)
  190.     
  191.     def testAtomLinkConstructInRSS(self):
  192.         urls = subscription.parseContent(ATOM_LINK_CONSTRUCT_IN_RSS)
  193.         self.assert_(len(urls) == 1)
  194.         self.assert_(urls[0] == SAMPLE_RSS_SUBSCRIPTION_URL_1)
  195.  
  196.     def testAtomLinkConstructInAtom(self):
  197.         urls = subscription.parseContent(ATOM_LINK_CONSTRUCT_IN_ATOM)
  198.         self.assert_(len(urls) == 1)
  199.         self.assert_(urls[0] == SAMPLE_ATOM_SUBSCRIPTION_URL_1)
  200.         
  201.     def testReflexiveAutoDiscoveryInRSS(self):
  202.         pageFile = file(REFLEXIVE_AUTO_DISCOVERY_PAGE_RSS_FILENAME, "w")
  203.         pageFile.write(REFLEXIVE_AUTO_DISCOVERY_PAGE_RSS)
  204.         pageFile.close()
  205.         urls = subscription.parseContent(REFLEXIVE_AUTO_DISCOVERY_IN_RSS)
  206.         self.assert_(len(urls) == 1)
  207.         self.assert_(urls[0] == SAMPLE_RSS_SUBSCRIPTION_URL_1)
  208.         os.remove(REFLEXIVE_AUTO_DISCOVERY_PAGE_RSS_FILENAME)
  209.  
  210.     def testReflexiveAutoDiscoveryInAtom(self):
  211.         pageFile = file(REFLEXIVE_AUTO_DISCOVERY_PAGE_ATOM_FILENAME, "w")
  212.         pageFile.write(REFLEXIVE_AUTO_DISCOVERY_PAGE_ATOM)
  213.         pageFile.close()
  214.         urls = subscription.parseContent(REFLEXIVE_AUTO_DISCOVERY_IN_ATOM)
  215.         self.assert_(len(urls) == 1)
  216.         self.assert_(urls[0] == SAMPLE_ATOM_SUBSCRIPTION_URL_1)
  217.         os.remove(REFLEXIVE_AUTO_DISCOVERY_PAGE_ATOM_FILENAME)
  218.     
  219.     def testFlatOPMLSubscriptions(self):
  220.         urls = subscription.parseContent(OPML_FLAT)
  221.         self.assert_(len(urls) == 4)
  222.         self.assert_(urls[0] == SAMPLE_RSS_SUBSCRIPTION_URL_1)
  223.         self.assert_(urls[1] == SAMPLE_ATOM_SUBSCRIPTION_URL_1)
  224.         self.assert_(urls[2] == SAMPLE_RSS_SUBSCRIPTION_URL_2)
  225.         self.assert_(urls[3] == SAMPLE_ATOM_SUBSCRIPTION_URL_2)
  226.  
  227.     def testNestedOPMLSubscriptions(self):
  228.         urls = subscription.parseContent(OPML_NESTED)
  229.         self.assert_(len(urls) == 4)
  230.         self.assert_(urls[0] == SAMPLE_RSS_SUBSCRIPTION_URL_1)
  231.         self.assert_(urls[1] == SAMPLE_ATOM_SUBSCRIPTION_URL_1)
  232.         self.assert_(urls[2] == SAMPLE_RSS_SUBSCRIPTION_URL_2)
  233.         self.assert_(urls[3] == SAMPLE_ATOM_SUBSCRIPTION_URL_2)
  234.  
  235. class TestFindSubscribeLinks (DemocracyTestCase):
  236.     def testDifferstHost(self):
  237.         url = 'http://youtoob.com'
  238.         self.assertEquals(subscription.findSubscribeLinks(url), 
  239.                 ('none', []))
  240.  
  241.     def testNoLinks(self):
  242.         url = 'http://subscribe.getdemocracy.com/'
  243.         self.assertEquals(subscription.findSubscribeLinks(url), 
  244.                 ('feed', []))
  245.  
  246.     def testLinkInPath(self):
  247.         url = 'http://subscribe.getdemocracy.com/http%3A//www.myblog.com/rss'
  248.         self.assertEquals(subscription.findSubscribeLinks(url), 
  249.                 ('feed', ['http://www.myblog.com/rss']))
  250.  
  251.     def testLinkInQuery(self):
  252.         url = ('http://subscribe.getdemocracy.com/'
  253.              '?url1=http%3A//www.myblog.com/rss')
  254.         self.assertEquals(subscription.findSubscribeLinks(url), 
  255.                 ('feed', ['http://www.myblog.com/rss']))
  256.  
  257.     def testMultipleLinksInQuery(self):
  258.         url = ('http://subscribe.getdemocracy.com/'
  259.              '?url1=http%3A//www.myblog.com/rss'
  260.              '&url2=http%3A//www.yourblog.com/atom'
  261.              '&url3=http%3A//www.herblog.com/scoobydoo')
  262.  
  263.         type, links = subscription.findSubscribeLinks(url)
  264.         self.assertEquals(type, 'feed')
  265.         self.assertEquals(set(links), set(['http://www.myblog.com/rss',
  266.             'http://www.yourblog.com/atom',
  267.             'http://www.herblog.com/scoobydoo' ]))
  268.  
  269.     def testQueryGarbage(self):
  270.         url = ('http://subscribe.getdemocracy.com/'
  271.              '?url1=http%3A//www.myblog.com/rss'
  272.              '&url2=http%3A//www.yourblog.com/atom'
  273.              '&url3=http%3A//www.herblog.com/scoobydoo'
  274.              '&foo=bar'
  275.              '&extra=garbage')
  276.  
  277.         type, links = subscription.findSubscribeLinks(url)
  278.         self.assertEquals(type, 'feed')
  279.         self.assertEquals(set(links), set(['http://www.myblog.com/rss',
  280.             'http://www.yourblog.com/atom',
  281.             'http://www.herblog.com/scoobydoo']))
  282.  
  283.     def testChannelGuideLinks(self):
  284.         url = ('http://subscribe.getdemocracy.com/channelguide.php'
  285.              '?url1=http%3A//www.mychannelguide.com/')
  286.         self.assertEquals(subscription.findSubscribeLinks(url), 
  287.                 ('guide', ['http://www.mychannelguide.com/']))
  288.  
  289.     def testDownloadLinks(self):
  290.         url = ('http://subscribe.getdemocracy.com/download.php'
  291.              '?url1=http%3A//www.myblog.com/videos/cats.ogm')
  292.         self.assertEquals(subscription.findSubscribeLinks(url), 
  293.                 ('download', ['http://www.myblog.com/videos/cats.ogm']))
  294.